import axios from "axios";
import { Message } from 'element-ui';
import router from "../router";
import store from '../store'

//前置拦截
axios.interceptors.request.use(config=>{
    return config;
})

//后置拦截
axios.interceptors.response.use(response=>{
    let resp = response.data;
    if(resp.code == 200){
        return response;
    }else {
        console.log(resp.code+"666")
        Message.error("输入了错误密码！",{duration: 2 * 1000});
        return Promise.reject(response.data.msg);
    }
},error => {
    if(error.response.data){
        error.message = error.response.data.msg
    }
    if(error.response.status == 401){
        store.commit("REMOVE_INFO");
        router.replace("/");
    }
    Message.error(error.message,{duration: 2 * 1000});
    return Promise.reject(error);
})

let base='';

//传送json格式的post请求
export const postRequest=(url,params)=>{
    return axios({
        method:'post',
        url:`${base}${url}`,
        data:params,

    })
}

//传送json格式的put请求
export const putRequest=(url,params)=>{
    return axios({
        method:'put',
        url:`${base}${url}`,
        data:params,
    })
}

//传送json格式的get请求
export const getRequest=(url,params)=>{
    return axios({
        method:'get',
        url:`${base}${url}`,
        data:params,
    })
}

//传送json格式的delete请求
export const deleteRequest=(url,params)=>{
    return axios({
        method:'delete',
        url:`${base}${url}`,
        data:params,
    })
}